<template>
  <div>
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item>
        <el-form-item label="分公司">
          <el-select v-model="formInline.branchId" v-if="isBranch" style="width: 180px" filterable>
            <el-option
              v-for="item in branchList"
              :label="item.branch_Name"
              :value="item.branch_Id">
            </el-option>
          </el-select>
          <el-input v-if="!isBranch" v-model="branchName" disabled></el-input>
        </el-form-item>
        <el-button type="primary" @click="onSubmit" style="margin-left: 20px">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="isLoading"
      :data="tableData"
      style="width: 100%"
      border>
      <el-table-column
        prop="sortId"
        label="置顶位"
        align="center">
      </el-table-column>
      <el-table-column
        prop="wordName"
        label="热词"
        align="center">
      </el-table-column>
      <el-table-column
        prop="updateTime"
        label="修改时间"
        align="center">
      </el-table-column>
      <el-table-column
        prop="userName"
        label="修改人"
        align="center"
        width="180">
      </el-table-column>
      <el-table-column
        label="操作"
        align="center">
        <template scope="scope">
          <el-button v-if="scope.row.wordId !== '0'" @click="handle(scope.$index, scope.row, $event)" size="small">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--修改弹窗-->
    <el-dialog title="修改热词" :visible.sync="editDialog">
      <el-form :model="editForm" style="" label-width="160px">
        <el-form-item label="分公司">
          <el-select v-model="editForm.branchIds" v-if="isBranch" multiple style="width: 180px" filterable>
            <el-option
              v-for="item in branchList"
              :label="item.branch_Name"
              :value="item.branch_Id">
            </el-option>
          </el-select>
          <el-input v-if="!isBranch" v-model="branchName" style="width: 180px" disabled></el-input>
        </el-form-item>
        <el-form-item label="热词" style="width: 300px">
          <el-input v-model="editForm.wordName"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="makeSure">提 交</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  import { rcwh } from '../../../config/proxy.js'
  import mixins from '../../../components/mixins.vue'
  export default {
    mixins: [mixins],
    data () {
      return {
        formInline: {
          branchId: ''
        },
        tableData: [],
        isLoading: false,
//      修改弹窗
        index: 2,
        editDialog: false,
        editForm: {
          branchIds: [],
          wordName: ''
        }
      }
    },
    methods: {
      onSubmit () {
        this.isLoading = true
        rcwh.query(this.formInline, (res) => {
          if (res.message === 'ok') {
            console.log(res)
//            if (res.content.hotword.length !== 5) {
//              var len = res.content.hotword.length
//              const lens = 5 - len
//              console.log(len, lens)
//              for (len; len <= lens; len++) {
//                const pushItem = {
//                  branchId: this.formInline.branchId,
//                  id: '',
//                  sortId: len + 1,
//                  updateTime: '',
//                  userId: '',
//                  userName: '',
//                  wordId: '1122',
//                  wordName: ''
//                }
//                res.content.hotword.push(pushItem)
//              }
//            }
            this.tableData = res.content.hotword.concat(res.content.hotword_sc)
            this.isLoading = false
          } else {
            this.$message({
              showClose: true,
              message: '查询失败',
              type: 'error'
            })
          }
        })
      },
      handle (index, row, event) {
        this.editForm.branchIds.push(row.branchId)
        this.editForm.sortId = row.sortId
        this.editForm.userId = this.userId
        this.editForm.userName = this.userName
        this.editDialog = true
      }, // 修改按钮
      makeSure () {
        rcwh.edit(this.editForm, (res) => {
          console.log(res)
          if (res.message === 'ok') {
            this.onSubmit()
          } else {
            this.$message({
              showClose: true,
              message: '修改失败',
              type: 'error'
            })
          }
        })
      }                 // 提交
    },
    computed: {
      userName: function (state) {
        return this.$store.state.user.info.user_name
      },
      userId: function (state) {
        return this.$store.state.user.info.user_id
      },
      branchName: function (state) {
        return this.$store.state.user.info.branchId === 1 ? '总公司' : this.$store.state.user.info.branchName
      },
      branchId: function (state) {
        return this.$store.state.user.info.branchId
      }
    },
    watch: {
      update_state (curVal, oldVal) {  // 监听 更新状态 查询list
        this.onSubmit()
      }
    },
    created () {
      this.formInline.branchId = this.branchId
      this.onSubmit()
    }
  }
</script>
<style>
  @media screen and (min-width:1000px) {
    .supplier-form{
      max-width: 600px;
    }
  }
  .el-form--inline div {
    display: inline-block;
    /*float: left;*/
  }
  .el-dialog__body {
    padding-top: 0;
  }
  .shuoming {
    line-height: 2;
    font-size: 16px;
    font-weight: 700;
    color: #1f2d3d;
  }
</style>
